package Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(urlPatterns = "/stmgoods/insert")
@MultipartConfig(location = "D:/img",maxFileSize = 100*1024*1024,maxRequestSize = 100*1024*1024)
public class GoodsInsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Part file = req.getPart("image");
        String cd = file.getHeader("Content-Disposition");
        String s = cd.substring(cd.lastIndexOf("=") + 2, cd.length() - 1);
//        String s = RandomStringUtils.randomAlphabetic(10) + System.nanoTime();
        file.write(s);
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String sql = "INSERT INTO `goods`(`name`,`price`,`image`) VALUES(?,?,?)";
        JdbcUtils.modify(sql,name,price,s);
        resp.sendRedirect("/Stm/goods-list.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
